// miniprogram/pages/home/delivery/delivery.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zpxxId:null,
    radioItems: [
      { name: 'cell standard', value: '0', checked: true },
      { name: 'cell standard', value: '1' }
    ],
    depItems:[],
    depItemsIndex:0,
    vitaeItems:[],
    vitaeItemsIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var depList = JSON.parse(options.depList) ;
    that.setData({ zpxxId: options.zpxxId})
    var depItems = []
    if (depList.length != 0) {
      depList.forEach(function (item, index) {
        depItems.push({
          name: item.text,
          value:index,
          checked: false
        })
      })
    }
    if (depItems.length!=0){
      depItems[0].checked = true;
      that.setData({
        depItems:depItems,
      })
    }
    wx.request({
      url: app.globalData.url + '/zpxx/getVitaeTitleByOpenid',
      data: {
        uniqueId: app.globalData.uniqueId
      },
      success: res => {
        var vitaeTitles = res.data
        var vitaeItems = []
        if (vitaeTitles.length != 0) {
          vitaeTitles.forEach(function (item, index) {
            vitaeItems.push({
              name: item,
              value: index,
              checked: false
            })
          })
        }
        if (vitaeItems.length != 0) {
          vitaeItems[0].checked = true;
          that.setData({
            vitaeItems: vitaeItems,
          })
        }
      }
    })
  },
  vitaeItemsChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var vitaeItems = this.data.vitaeItems;
    for (var i = 0, len = vitaeItems.length; i < len; ++i) {
      vitaeItems[i].checked = vitaeItems[i].value == e.detail.value;
    }
    this.setData({
      vitaeItems: vitaeItems,
      vitaeItemsIndex: e.detail.value,
    });
  },
  depItemsChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var depItems = this.data.depItems;
    for (var i = 0, len = depItems.length; i < len; ++i) {
      depItems[i].checked = depItems[i].value == e.detail.value;
    }

    this.setData({
      depItems: depItems,
      depItemsIndex: e.detail.value,
    });
  },
  formSubmit:function(){
    var that = this
    if(that.data.depItems.length==0){
      wx.showModal({
        title: '提示',
        content: "确认使用简历：" + that.data.vitaeItems[that.data.vitaeItemsIndex].name,
        success: function (res) {
          if (res.confirm) {
            that.delivery(0);
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: "确认选择部门：" + that.data.depItems[that.data.depItemsIndex].name +
          ",使用简历：" + that.data.vitaeItems[that.data.vitaeItemsIndex].name,
        success: function (res) {
          if (res.confirm) {
            that.delivery(1);
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
    } 
  },
  delivery:function(e){
    var that = this
    console.log(e)
    if(e==0){
      wx.request({
        url: app.globalData.url + '/zpxx/delivery',
        data: {
          uniqueId: app.globalData.uniqueId,
          zpxxId: that.data.zpxxId,
          title: that.data.vitaeItems[that.data.vitaeItemsIndex].name
        },
        success: res => {
          if (res.data.code == 0) {
            wx.showModal({
              title: '失败',
              content: '请联系管理员',
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '投递成功，返回页面',
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '../my/delivery',
                  })
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }else{
      wx.request({
        url: app.globalData.url + '/zpxx/delivery',
        data: {
          uniqueId: app.globalData.uniqueId,
          zpxxId: that.data.zpxxId,
          depName: that.data.depItems[that.data.depItemsIndex].name,
          title: that.data.vitaeItems[that.data.vitaeItemsIndex].name
        },
        success: res => {
          if (res.data.code == 0) {
            wx.showModal({
              title: '失败',
              content: '请联系管理员',
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '投递成功，返回页面',
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '../my/delivery',
                  })
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})